home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / PASCAL / 0187.ZIP / BJGAME.PAS < prev    next >
Pascal/Delphi Source File  |  1985-01-20  |  9KB  |  417 lines

  1. program bjgame;
  2.  
  3. const
  4.    decksize    = 52;
  5.    maxhandsize = 5;
  6.    mincards    = 5;
  7.    dealerstays = 17;
  8.    busted      = 21;
  9.    startamount = 100;
  10.    minbet      = 2;
  11.    maxbet      = 200;
  12.  
  13. type
  14.    cardsuit = (spades, hearts, clubs, diamonds);
  15.    cardvalue = (duece, three, four, five, six, seven, eight, nine, ten,
  16.                 jack, queen, king, ace);
  17.    cardstate = (picked, indeck);
  18.    card = record
  19.              suit : cardsuit;
  20.              value : cardvalue;
  21.              state : cardstate;
  22.           end;
  23.    hand = array [1..maxhandsize] of card;
  24.  
  25. var
  26.    deck         : array[1..decksize] of card;
  27.    cardsleft    : integer;
  28.    suitname     : array[cardsuit] of string[8];
  29.    valuename    : array[cardvalue] of string[5];
  30.    countvalue   : array[cardvalue] of integer;
  31.    player       : hand;
  32.    dealer       : hand;
  33.    money        : integer;
  34.    bet          : integer;
  35.    curcard      : integer;
  36.  
  37. (*
  38.  * write the suit and value of a card
  39.  *)
  40.  
  41. procedure printcard(acard: card);
  42.  
  43. begin
  44.    write('the ',valuename[acard.value]);
  45.    writeln(' of ',suitname[acard.suit]);
  46. end;
  47.  
  48. (*
  49.  * asks for intructions
  50.  *)
  51.  
  52. procedure instructions;
  53.  
  54. var
  55.    response : char;
  56.  
  57. begin
  58.    writeln(' ':15,'Blackjack for one');
  59.    writeln('Do you want instructions? ');
  60.    readln(response);
  61.    if (response = 'y') or (response = 'Y') then
  62.       begin
  63.       writeln('This program plays a simple version of blackjack. Neither');
  64.       writeln('splitting, nor modification of the bet after the hand has');
  65.       writeln('been dealt is allowed.');
  66.       end;
  67.    writeln;
  68. end;
  69.  
  70. (*
  71.  * returns true if the card c is in the hand
  72.  *)
  73.  
  74. function inhand(c: card; whose: hand): boolean;
  75.  
  76. var
  77.    handindex : integer;
  78.  
  79. begin
  80.    inhand := false;
  81.    for handindex := 1 to maxhandsize do
  82.       if ((c.suit = whose[handindex].suit) and
  83.          (c.value = whose[handindex].value)) then
  84.          begin
  85.          inhand := true;
  86.          end;
  87. end;
  88.  
  89. (*
  90.  * returns a random index into the deck
  91.  *)
  92.  
  93. function randcard(l: integer) : integer;
  94.  
  95. begin
  96.    randcard := 1 + random(l);
  97. end;
  98.  
  99. (*
  100.  * removes all cards from the argument hand
  101.  *)
  102.  
  103. procedure clearhand(var ahand: hand);
  104.  
  105. var
  106.    handindex : integer;
  107.  
  108. begin
  109.    for handindex := 1 to maxhandsize do
  110.       ahand[handindex].state := indeck;
  111. end;
  112.  
  113. (*
  114.  * initialize the names of the suits and values
  115.  *)
  116.  
  117. procedure initialize;
  118.  
  119. var
  120.    i       : integer;
  121.    cardval : cardvalue;
  122.  
  123. begin
  124.    instructions;
  125.    clearhand(player);
  126.    clearhand(dealer);
  127.    money := startamount;
  128.    cardsleft := 0;
  129.    i := 2;
  130.    for cardval := duece to ten do
  131.       begin
  132.       countvalue[cardval] := i;
  133.       i := i + 1;
  134.       end;
  135.    for cardval := jack to king do
  136.       countvalue[cardval] := 10;
  137.    countvalue[ace] := 11;
  138.  
  139.    valuename[duece] := 'two';
  140.    valuename[three] := 'three';
  141.    valuename[four]  := 'four';
  142.    valuename[five]  := 'five';
  143.    valuename[six]   := 'six';
  144.    valuename[seven] := 'seven';
  145.    valuename[eight] := 'eight';
  146.    valuename[nine]  := 'nine';
  147.    valuename[ten]   := 'ten';
  148.    valuename[jack]  := 'jack';
  149.    valuename[queen] := 'queen';
  150.    valuename[king]  := 'king';
  151.    valuename[ace]   := 'ace';
  152.    suitname[diamonds] := 'diamonds';
  153.    suitname[spades]   := 'spades';
  154.    suitname[hearts]   := 'hearts';
  155.    suitname[clubs]    := 'clubs';
  156.    randomize;
  157. end;
  158.  
  159. (*
  160.  * shuffles the cards that are not in either player's hand. the initial shuffle
  161.  * does all the cards because both hands start empty.
  162.  *)
  163.  
  164. procedure shuffle;
  165.  
  166. var
  167.    asuit  : cardsuit;
  168.    avalue : cardvalue;
  169.    i      : integer;
  170.  
  171. (*
  172.  * exchange the cards at the two positions in the deck
  173.  *)
  174.  
  175. procedure swapcard(first, second : integer);
  176.  
  177. var
  178.    tempcard : card;
  179.  
  180. begin
  181.    tempcard := deck[first];
  182.    deck[first] := deck[second];
  183.    deck[second] := tempcard;
  184. end;
  185.  
  186. begin
  187.    i := 1;
  188.    for asuit := spades to diamonds do
  189.       for avalue := duece to ace do
  190.          with deck[i] do
  191.          begin
  192.          suit := asuit;
  193.          value := avalue;
  194.          if not (inhand(deck[i], player) or inhand(deck[i], dealer)) then
  195.             begin
  196.             state := indeck;
  197.             i := i + 1;
  198.             end;
  199.          end;
  200.    curcard := 0;
  201.    cardsleft := i - 1;
  202.    writeln('*** ',cardsleft:1,' cards left.');
  203.    for i := 1 to cardsleft do
  204.       swapcard(i, randcard(cardsleft));
  205. end;
  206.  
  207. (*
  208.  * returns the index into the deck of the next card. calls shuffle if deck
  209.  * is nearly finished.
  210.  *)
  211.  
  212. function pickcard : integer;
  213.  
  214. begin
  215.    if cardsleft < mincards then
  216.       begin
  217.       writeln('Reshuffling ...');
  218.       shuffle;
  219.       end;
  220.    curcard := curcard + 1;
  221.    deck[curcard].state := picked;
  222.    cardsleft := cardsleft - 1;
  223.    pickcard := curcard;
  224. end;
  225.  
  226. (*
  227.  * determines the sum of the values in a hand. a card's state must be
  228.  * 'picked' for it to be included. aces are assumed to be 11. if the
  229.   * count is over 21 and there are aces in it, as many as are needed
  230.   * will be devalued to 1.
  231.   *)
  232.  
  233. function countcards(someone: hand): integer;
  234.  
  235. var
  236.    handindex, sum, numaces : integer;
  237.  
  238. begin
  239.    sum := 0;
  240.    numaces := 0;
  241.    for handindex := 1 to maxhandsize do
  242.       if someone[handindex].state = picked then
  243.          with someone[handindex] do
  244.          begin
  245.          if value = ace then
  246.             numaces := numaces +1;
  247.          sum := sum + countvalue[value];
  248.          end;
  249.    while (numaces > 0) and (sum > busted) do
  250.       begin
  251.       numaces := numaces - 1;
  252.       sum := sum - 10;
  253.       end;
  254.    countcards := sum;
  255. end;
  256.  
  257. (*
  258.  * returns true if the argument hand is a blackjack
  259.  *)
  260.  
  261. function blackjack(someone: hand): boolean;
  262.  
  263. begin
  264.    blackjack := ((countvalue[someone[1].value] = 10) and
  265.                  (countvalue[someone[2].value] = 11)) or
  266.                 ((countvalue[someone[1].value] = 11) and
  267.                  (countvalue[someone[2].value] = 10));
  268. end;
  269.  
  270. procedure getbet;
  271.  
  272. const
  273.    betprompt = 'Size of bet (0 to end)? ';
  274.  
  275. begin
  276.    write(betprompt);
  277.    readln(bet);
  278.    while not (bet in [0,minbet..maxbet]) or (bet > money) do
  279.       begin
  280.       write('A bet must be between ');
  281.       writeln(minbet:1,' and ',maxbet:1);
  282.       writeln('and must be no larger than the amount of money you have.');
  283.       writeln('Enter 0 to leave.');
  284.       write(betprompt);
  285.       readln(bet);
  286.       end;
  287.    if bet = 0 then
  288.       begin
  289.       writeln('You have quit with $',money:1,'.');
  290.       halt;
  291.       end;
  292. end;
  293.  
  294. (*
  295.  * deals the cards tpo both participants for this hand
  296.  *)
  297.  
  298. procedure dealhands;
  299.  
  300. begin
  301.    player[1] := deck[pickcard];
  302.    dealer[1] := deck[pickcard];
  303.    player[2] := deck[pickcard];
  304.    dealer[2] := deck[pickcard];
  305.    write('You drew ');
  306.    printcard(player[1]);
  307.    write('and ');
  308.    printcard(player[2]);
  309.    writeln;
  310.    write('The dealer''s up card is ');
  311.    printcard(dealer[2]);
  312. end;
  313.  
  314. (*
  315.  * asks the player if more cards are wanted.
  316.  *)
  317.  
  318. procedure playertakes;
  319.  
  320. var
  321.    atcard : integer;
  322.    answer : char;
  323.  
  324. begin
  325.    atcard := 3;
  326.    answer := 'h';
  327.    while (atcard <= maxhandsize) and (countcards(player) < busted) and
  328.          ((answer = 'h') or (answer = 'H')) do
  329.       begin
  330.       writeln('Your count is ',countcards(player));
  331.       write('Hit or stay? ');
  332.       readln(answer);
  333.       if (answer = 'h') or (answer = 'H') then
  334.          begin
  335.          player[atcard] := deck[pickcard];
  336.          write('You drew ');
  337.          printcard(player[atcard]);
  338.          atcard := atcard + 1;
  339.          end;
  340.       end;
  341.    if (countcards(player) < busted) and (atcard > maxhandsize) then
  342.       writeln('You can take only ',maxhandsize:1,' cards.');
  343. end;
  344.  
  345. procedure dealertakes;
  346.  
  347. var
  348.    atcard : integer;
  349.  
  350. begin
  351.    write('Dealer''s hole card is ');
  352.    printcard(dealer[1]);
  353.    atcard := 3;
  354.    while (atcard <= maxhandsize) and (countcards(dealer) < dealerstays) do
  355.       begin
  356.       dealer[atcard] := deck[pickcard];
  357.       write('Dealer drew ');
  358.       printcard(dealer[atcard]);
  359.       atcard := atcard + 1;
  360.       end;
  361. end;
  362.  
  363. procedure whowon;
  364.  
  365. begin
  366.    writeln('Dealer has ',countcards(dealer):1,'.');
  367.    if blackjack(dealer) then
  368.       begin
  369.       write('Dealer got a blackjack.');
  370.       money := money - bet;
  371.       end
  372.    else if blackjack(player) then
  373.       begin
  374.       write('Your blackjack wins!');
  375.       money := money + bet;
  376.       end
  377.    else if countcards(player) > busted then
  378.       begin
  379.       write('You busted.');
  380.       if countcards(dealer) > busted then
  381.           write(' So did the dealer. No payout.')
  382.       else
  383.           money := money - bet;
  384.       end
  385.    else if countcards(dealer) > busted then
  386.       begin
  387.       write('Dealer busts.');
  388.       money := money + bet;
  389.       end
  390.    else if countcards(dealer) = countcards(player) then
  391.       write('Push.')
  392.    else if countcards(dealer) > countcards(player) then
  393.       money := money - bet
  394.    else
  395.       money := money + bet;
  396.    writeln(' You now have $',money:1);
  397. end;
  398.  
  399. begin
  400.    initialize;
  401.    shuffle;
  402.    repeat
  403.       getbet;
  404.       clrscr;
  405.       dealhands;
  406.       if not blackjack(player) then
  407.          playertakes;
  408.       dealertakes;
  409.       whowon;
  410.       clearhand(player);
  411.       clearhand(dealer);
  412.    until money <= 0;
  413.    writeln('You have run out of money.');
  414. end.
  415.  
  416.  
  417.